﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using Lamex.Library.CommonClass;
using DevExpress.Utils;
using Lamex.Interface;
using Lamex.SystemManager.Model;
using Lamex.SystemManager.BLL;
using Lamex.Base;


namespace Lamex.SystemManager.UI
{
    public partial class frmButtonList : frmBaseDataForm
    {
        //public string IsEdit = "";
        //public string KeysName = "";
        public frmButtonList()
        {
            InitializeComponent();
            _SummaryView = new DevGridView(gridView2);
            gridView2.DoubleClick += new EventHandler(OnGridViewDoubleClick); //主表DoubleClict
            frmGridViewCust.RegisterGrid(gridView2);
            DevStyle.SetGridControlLayout(gcView, false);//表格设置   

            DevStyle.SetSummaryGridViewLayout(gridView2);
            DevStyle.SetDetailGridViewLayout(gridView2);
            ShowSummary(); //下载显示数据.
            tpSummary = xtraTabPage1;
            tcBusiness = xtraTabControl1;
            tpSummary.PageVisible = true;
            //tpDetail.PageVisible = false;
            tcBusiness.ShowTabHeader = DefaultBoolean.True;
            tcBusiness.SelectedTabPage = tpSummary;
        }
        private void ShowSummary()
        {
            try
            {

                xtraTabPage2.PageEnabled = false;
                xtraTabPage1.PageEnabled = true;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void frmButtonList_Load(object sender, EventArgs e)
        {
            try { this.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理
        }


        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        public override void DoSave(IButtonInfo sender)
        {
            if (txtEditButtonID.Alter()) return;
            if (txtEditButtonName.Alter()) return;
            if (txtEditEventHandle.Alter()) return;
            if (IsEdit == "新增")
            {
                Sys_ButtonListInfo info = BaseFactory<Sys_ButtonList>.Instance.GetEntriyList(" 1=1 and ButtonID='" + txtEditButtonID.Text + "'").FirstOrDefault();
                if (info != null)
                {
                    MessageBox.Show("按钮ID已存在！", "提示");
                    return;
                }
                Sys_ButtonListInfo model = new Sys_ButtonListInfo();
                model.ButtonID = txtEditButtonID.Text;
                model.ButtonName = txtEditButtonName.Text;
                model.EventHandler = txtEditEventHandle.Text;
                

                if (BaseFactory<Sys_ButtonList>.Instance.Insert(model))
                {
                    MessageBox.Show("保存存成功！");

                }
                else
                {
                    MessageBox.Show("保存存失败！");
                }

            }
            if (IsEdit == "编辑")
            {
                Sys_ButtonListInfo model = new Sys_ButtonListInfo();
                model.IDNO = int.Parse(KeysName);
                model.ButtonID = txtEditButtonID.Text;
                model.ButtonName = txtEditButtonName.Text;
                model.EventHandler = txtEditEventHandle.Text;
                
                if (BaseFactory<Sys_ButtonList>.Instance.Update(model))
                {
                    MessageBox.Show("保存存成功！");

                }
                else
                {
                    MessageBox.Show("保存存失败！");
                }
            }

            xtraTabPage2.PageEnabled = false;
            xtraTabPage1.PageEnabled = true;
            DoView(null);
            base.DoSave(sender);
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        public override void DoEdit(IButtonInfo sender)
        {
            if (gridView2.SelectedRowsCount == 0)
            {
                MessageBox.Show("【没有选中要删除要修改的数据！】");
                return;
            }
            int m = gridView2.FocusedRowHandle;
            DataRow dr = gridView2.GetDataRow(m);
            KeysName = dr["IDNO"].ToString();
            //***********************加载数据****************************//
            txtEditButtonID.Text = dr["ButtonID"].ToString();
            txtEditButtonName.Text = dr["ButtonName"].ToString();
            txtEditEventHandle.Text = dr["EventHandler"].ToString();
            
            base.DoEdit(sender);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        public override void DoDelete(IButtonInfo sender)
        {
            if (gridView2.SelectedRowsCount == 0)
            {
                MessageBox.Show("【没有选中要删除的数据，请选择好数据！】");
                return;
            }
            int m = gridView2.FocusedRowHandle;
            DataRow dr = gridView2.GetDataRow(m);
            if (dr != null)
            {
                if (BaseFactory<Sys_ButtonList>.Instance.Delete(dr["IDNO"].ToString()))
                {
                    MessageBox.Show("【数据删除成功！】");

                }
                else
                {
                    MessageBox.Show("【数据删除失败！】");

                }
            }
            DoView(null);
            base.DoDelete(sender);
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        public override void DoExport(IButtonInfo sender)
        {

            base.DoExport(sender);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        public override void DoView(IButtonInfo sender)
        {
            string sqlWhere = "";
            string sql = " select * from Sys_ButtonList where 1=1 ";
            if (!string.IsNullOrEmpty(txtButtonID.Text))
            {
                sqlWhere += " and ButtonID like '%" + txtButtonID.Text + "%'";
            }
            if (!string.IsNullOrEmpty(txtButtonName.Text))
            {
                sqlWhere += " and ButtonName like '%" + txtButtonName.Text + "%'";
            }
            if (!string.IsNullOrEmpty(txtEventHanle.Text))
            {
                sqlWhere += " and EventHandler = '" + txtEventHanle.Text + "'";
            }

            sql = sql + sqlWhere;
            DataTable dt = BaseFactory<Sys_ButtonList>.Instance.GetDataTableBySql(sql);
            DoBindingSummaryGrid(dt);
            base.DoView(sender);
        }
        
        //public override void DoReturn(IButtonInfo sender)
        //{
        //    xtraTabPage1.PageEnabled = true;
        //    xtraTabPage2.PageEnabled = false;
        //    IsEdit = "";
        //    base.DoReturn(sender);
        //}
    }
}